package com.hod.client.frontend;

import com.google.gwt.user.client.Window;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.PagingToolbar;
import com.gwtext.client.widgets.ToolTip;
import com.gwtext.client.widgets.form.Field;
import com.gwtext.client.widgets.form.NumberField;
import com.gwtext.client.widgets.form.event.FieldListenerAdapter;
import com.gwtext.client.widgets.grid.BaseColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.GridPanel;
import com.gwtext.client.widgets.grid.event.GridRowListenerAdapter;
import com.gwtext.client.widgets.layout.AnchorLayout;
import com.gwtext.client.widgets.menu.BaseItem;
import com.gwtext.client.widgets.menu.Item;
import com.gwtext.client.widgets.menu.Menu;
import com.gwtext.client.widgets.menu.event.BaseItemListenerAdapter;
import com.hod.client.backend.orm.client.GearsDataStore;

public abstract class AbstractGrid extends GridPanel {
	
	public AbstractGrid() {
		super();
		setLayout(new AnchorLayout());
		setHeight("100%");
		// TODO Auto-generated constructor stub
	}

	protected Menu gridMenu = null;
	protected ColumnModel columnModel = null;
	protected BaseColumnConfig[] columns = null;
	protected Store store;
	protected GearsDataStore ds = null;
	protected final int numberRecords = 25;
	
	
	protected abstract void getData();
	protected abstract void getData(String id);
	
	public Store getStore(){
		return store;
	}
	
    protected ColumnModel getColumnConfigs() {
        ColumnModel columnModel = new ColumnModel(columns);
        return columnModel;
    }
	public void createPager() {
   	    final PagingToolbar pagingToolbar = new PagingToolbar(getStore());
        pagingToolbar.setDisabled(false);
        pagingToolbar.setPageSize(numberRecords);
        pagingToolbar.setDisplayInfo(true);
        pagingToolbar.setDisplayMsg("Displaying clients {0} - {1} of {2}");
        pagingToolbar.setEmptyMsg("No records to display");
        NumberField pageSizeField = new NumberField();
        pageSizeField.setWidth(40);
        pageSizeField.setSelectOnFocus(true);
        pageSizeField.addListener(new FieldListenerAdapter() {
            public void onSpecialKey(Field field, EventObject e) {
                if (e.getKey() == EventObject.ENTER) {
                    int pageSize = Integer.parseInt(field.getValueAsString());
                    pagingToolbar.setPageSize(pageSize);
                }
            }
        });
        ToolTip toolTip = new ToolTip("Enter page size");
        toolTip.applyTo(pageSizeField);

        pagingToolbar.addField(pageSizeField);
        setBottomToolbar(pagingToolbar);
		
	}
	protected void createGridMenu() { 
		   gridMenu = new Menu(); 
		 //create context menu along with associated event handlers for each menu item private void createGridMenu() { gridMenu = new Menu("grid-ctx"); gridMenu.addItem(new Item("View in new Tab", new ItemConfig() { { setIconCls("new-tab"); setBaseItemListener(new BaseItemListenerAdapter() { public void onClick(BaseItem item, EventObject e) { viewer.openTab(ctxRecord); } }); } }));
		   gridMenu.addItem(new Item("Refresh",new BaseItemListenerAdapter() { 
					           public void onClick(BaseItem item, EventObject e) { 
					        	   getStore().reload(); 
					        	} 
					         }));  
    }
	public void createPopUp(){
		 //add listener for context menu when user right clicks on a row 
		addGridRowListener(new GridRowListenerAdapter() { 
			public void onRowContextMenu(GridPanel grid, int rowIndex, EventObject e) {			  
			  if (gridMenu == null){ createGridMenu(); } 
			  e.stopEvent(); 
			  gridMenu.showAt(e.getXY()); 
			 } 
		});
	}
	
	 public void refresh(String id){
		    //this.clear();
	    	getData(id);
	    	if ((store!=null) && (columnModel!=null)) reconfigure(store, columnModel);
	    	if (this.getView()!=null)
	            this.getView().refresh(true);
	    }

		public void refresh() {
			this.clear();
	    	getData(); 
	    	reconfigure(store, columnModel);
	        this.getView().refresh(true);		
		}
}
